package com.jshx.sz.gdjt.activity;

import java.util.HashMap;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.text.method.DigitsKeyListener;
import android.view.InputEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jshx.sz.gdjt.R;
import com.jshx.sz.gdjt.activity.listener.MyTextWatcher;
import com.jshx.sz.gdjt.control.TextEditView;
import com.jshx.sz.gdjt.db.DataBaseUtil;
/**
 * 定值Fragment
 * @author xiangchangan
 * @time 2013-08-23
 */
public class ConstantFrame extends Fragment implements OnClickListener
{
	//
	private LinearLayout constant_tab_layout;
	
	//
	private TextView constant_tab1;
	
	//
	private TextView constant_tab2;
	
	//
	private TextView constant_area1_title;
	
	//
	private TextView constant_area2_title;
	
	//
	private TextEditView constant_area1_input1;
	
	//
	private TextEditView constant_area1_input2;
	
	//
	private TextEditView constant_area1_input3;
	
	//
	private TextEditView constant_area1_input4;
	
	//
	private TextEditView constant_area2_input1;
	
	//
	private TextEditView constant_area2_input2;
	
	//
	private TextEditView constant_area2_input3;
	
	//
	private TextEditView constant_area2_input4;
	
	//页面数据
	private HashMap<String, String> data;
	
	//type 1:定时限过流保护    2:光纤纵差保护
	private int type = 1;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.constant_fragment, null);
		initView(view);
		return view;
	}

	/**
	 * 组件实例化初始化
	 * @param view
	 */
	private void initView(View view) {
		constant_tab_layout = (LinearLayout)view.findViewById(R.id.constant_tab_layout);
		constant_tab1 = (TextView)view.findViewById(R.id.constant_tab1);
		constant_tab2 = (TextView)view.findViewById(R.id.constant_tab2);
		constant_area1_title = (TextView)view.findViewById(R.id.constant_area1_title);
		constant_area2_title = (TextView)view.findViewById(R.id.constant_area2_title);
		constant_area1_input1 = (TextEditView)view.findViewById(R.id.constant_area1_input1);
		constant_area1_input2 = (TextEditView)view.findViewById(R.id.constant_area1_input2);
		constant_area1_input3 = (TextEditView)view.findViewById(R.id.constant_area1_input3);
		constant_area1_input4 = (TextEditView)view.findViewById(R.id.constant_area1_input4);
		constant_area2_input1 = (TextEditView)view.findViewById(R.id.constant_area2_input1);
		constant_area2_input2 = (TextEditView)view.findViewById(R.id.constant_area2_input2);
		constant_area2_input3 = (TextEditView)view.findViewById(R.id.constant_area2_input3);
		constant_area2_input4 = (TextEditView)view.findViewById(R.id.constant_area2_input4);
		constant_tab1.setOnClickListener(this);
		constant_tab2.setOnClickListener(this);
		
//		constant_area1_input1.setEditType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
//		constant_area1_input2.setEditType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
//		constant_area1_input3.setEditType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
//		constant_area1_input4.setEditType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
//		constant_area2_input1.setEditType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
//		constant_area2_input2.setEditType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
//		constant_area2_input3.setEditType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
//		constant_area2_input4.setEditType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
		
	
		
		
		
		//录入数字设置 准备
		DigitsKeyListener numericOnlyListener = new DigitsKeyListener(false,true);
		//上限设置 准备
		MyTextWatcher tw11=new MyTextWatcher(constant_area1_input1.getEdit());
		MyTextWatcher tw12=new MyTextWatcher(constant_area1_input2.getEdit());
		MyTextWatcher tw13=new MyTextWatcher(constant_area1_input3.getEdit());
		MyTextWatcher tw14=new MyTextWatcher(constant_area1_input4.getEdit());
		
		MyTextWatcher tw21=new MyTextWatcher(constant_area2_input1.getEdit());
		MyTextWatcher tw22=new MyTextWatcher(constant_area2_input2.getEdit());
		MyTextWatcher tw23=new MyTextWatcher(constant_area2_input3.getEdit());
		MyTextWatcher tw24=new MyTextWatcher(constant_area2_input4.getEdit());
		
		//设置 数字限制和上限
		constant_area1_input1.getEdit().setKeyListener(numericOnlyListener);
		constant_area1_input1.getEdit().addTextChangedListener(tw11 );
		constant_area1_input2.getEdit().setKeyListener(numericOnlyListener);
		constant_area1_input2.getEdit().addTextChangedListener(tw12 );
		constant_area1_input3.getEdit().setKeyListener(numericOnlyListener);
		constant_area1_input3.getEdit().addTextChangedListener(tw13 );
		constant_area1_input4.getEdit().setKeyListener(numericOnlyListener);
		constant_area1_input4.getEdit().addTextChangedListener(tw14 );
		
		constant_area2_input1.getEdit().setKeyListener(numericOnlyListener);
		constant_area2_input1.getEdit().addTextChangedListener(tw21 );
		constant_area2_input2.getEdit().setKeyListener(numericOnlyListener);
		constant_area2_input2.getEdit().addTextChangedListener(tw22 );
		constant_area2_input3.getEdit().setKeyListener(numericOnlyListener);
		constant_area2_input3.getEdit().addTextChangedListener(tw23 );
		constant_area2_input4.getEdit().setKeyListener(numericOnlyListener);
		constant_area2_input4.getEdit().addTextChangedListener(tw24);
		
		data = ((TaskActivityGroup)getActivity()).getDataFragment2();
	}
	
	@Override
	public void onResume() {
		setViewType(false);
		super.onResume();
	}
	
	/**
	 * 根据类型显示相应组件
	 * type 1:定时限过流保护    2:光纤纵差保护
	 */
	private void setViewType(boolean needSave2Tab)
	{
		if(type == 1)
		{
			constant_area1_title.setText("过流I段");
			constant_area2_title.setText("零序I段");
			constant_area1_input1.setTitle("整定电流A");
			constant_area1_input2.setTitle("整定时限S");
			constant_area1_input3.setTitle("动作电流A");
			constant_area1_input4.setTitle("动作时限S");
			constant_area2_input1.setTitle("整定电流A");
			constant_area2_input2.setTitle("整定时限S");
			constant_area2_input3.setTitle("动作电流A");
			constant_area2_input4.setTitle("动作时限S");
			if(needSave2Tab)
			{
				if(data == null)
				{
					data = new HashMap<String, String>();
				}
				System.out.println(constant_area1_input1.getEditValue());
				data.put(DataBaseUtil.task_detail_type2_table_clos[18], constant_area1_input1.getEditValue());
				data.put(DataBaseUtil.task_detail_type2_table_clos[19], constant_area1_input2.getEditValue());
				data.put(DataBaseUtil.task_detail_type2_table_clos[20], constant_area1_input3.getEditValue());
				data.put(DataBaseUtil.task_detail_type2_table_clos[21], constant_area1_input4.getEditValue());
				data.put(DataBaseUtil.task_detail_type2_table_clos[22], constant_area2_input1.getEditValue());
				data.put(DataBaseUtil.task_detail_type2_table_clos[23], constant_area2_input2.getEditValue());
				data.put(DataBaseUtil.task_detail_type2_table_clos[24], constant_area2_input3.getEditValue());
				data.put(DataBaseUtil.task_detail_type2_table_clos[25], constant_area2_input4.getEditValue());
			}
			clearData();
			if(data != null)
			{
				constant_area1_input1.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[10]));
				constant_area1_input2.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[11]));
				constant_area1_input3.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[12]));
				constant_area1_input4.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[13]));
				constant_area2_input1.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[14]));
				constant_area2_input2.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[15]));
				constant_area2_input3.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[16]));
				constant_area2_input4.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[17]));
			}
		}
		else if(type == 2)
		{
			constant_area1_title.setText("整定值");
			constant_area2_title.setText("动作值");
			constant_area1_input1.setTitle("启动电流值");
			constant_area1_input2.setTitle("斜率1");
			constant_area1_input3.setTitle("斜率2");
			constant_area1_input4.setTitle("两斜点拐点");
			constant_area2_input1.setTitle("启动电流值");
			constant_area2_input2.setTitle("斜率1");
			constant_area2_input3.setTitle("斜率2");
			constant_area2_input4.setTitle("两斜点拐点");
			
			
			if(needSave2Tab)
			{
				if(data == null)
				{
					data = new HashMap<String, String>();
				}
				data.put(DataBaseUtil.task_detail_type2_table_clos[10], constant_area1_input1.getEditValue());
				data.put(DataBaseUtil.task_detail_type2_table_clos[11], constant_area1_input2.getEditValue());
				data.put(DataBaseUtil.task_detail_type2_table_clos[12], constant_area1_input3.getEditValue());
				data.put(DataBaseUtil.task_detail_type2_table_clos[13], constant_area1_input4.getEditValue());
				data.put(DataBaseUtil.task_detail_type2_table_clos[14], constant_area2_input1.getEditValue());
				data.put(DataBaseUtil.task_detail_type2_table_clos[15], constant_area2_input2.getEditValue());
				data.put(DataBaseUtil.task_detail_type2_table_clos[16], constant_area2_input3.getEditValue());
				data.put(DataBaseUtil.task_detail_type2_table_clos[17], constant_area2_input4.getEditValue());
			}
			
			clearData();
			constant_area1_input1.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[18]));
			constant_area1_input2.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[19]));
			constant_area1_input3.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[20]));
			constant_area1_input4.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[21]));
			constant_area2_input1.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[22]));
			constant_area2_input2.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[23]));
			constant_area2_input3.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[24]));
			constant_area2_input4.setValue(data.get(DataBaseUtil.task_detail_type2_table_clos[25]));
		}
	}
	
	/**
	 * 
	 */
	private void clearData()
	{
		constant_area1_input1.setValue("");
		constant_area1_input2.setValue("");
		constant_area1_input3.setValue("");
		constant_area1_input4.setValue("");
		constant_area2_input1.setValue("");
		constant_area2_input2.setValue("");
		constant_area2_input3.setValue("");
		constant_area2_input4.setValue("");
	}

	@Override
	public void onClick(View v) 
	{
		int viewId = v.getId();
		switch (viewId) {
		case R.id.constant_tab1:
			if(type != 1)
			{
				constant_tab_layout.setBackgroundResource(R.drawable.constant_type1);
				type = 1;
				setViewType(true);
			}	
			break;
		case R.id.constant_tab2:
			if(type != 2)
			{
				constant_tab_layout.setBackgroundResource(R.drawable.constant_type2);
				type = 2;
				setViewType(true);
			}
			break;	
		default:
			break;
		}
	}

	/**
	 * 获取页面数据
	 * @return
	 */
	public HashMap<String, String> getData() 
	{
		if(data == null)
		{
			data = new HashMap<String, String>();
		}
		if(type == 1)
		{
			data.put(DataBaseUtil.task_detail_type2_table_clos[10], constant_area1_input1.getEditValue());
			data.put(DataBaseUtil.task_detail_type2_table_clos[11], constant_area1_input2.getEditValue());
			data.put(DataBaseUtil.task_detail_type2_table_clos[12], constant_area1_input3.getEditValue());
			data.put(DataBaseUtil.task_detail_type2_table_clos[13], constant_area1_input4.getEditValue());
			data.put(DataBaseUtil.task_detail_type2_table_clos[14], constant_area2_input1.getEditValue());
			data.put(DataBaseUtil.task_detail_type2_table_clos[15], constant_area2_input2.getEditValue());
			data.put(DataBaseUtil.task_detail_type2_table_clos[16], constant_area2_input3.getEditValue());
			data.put(DataBaseUtil.task_detail_type2_table_clos[17], constant_area2_input4.getEditValue());
		}
		else if(type == 2)
		{
			data.put(DataBaseUtil.task_detail_type2_table_clos[18], constant_area1_input1.getEditValue());
			data.put(DataBaseUtil.task_detail_type2_table_clos[19], constant_area1_input2.getEditValue());
			data.put(DataBaseUtil.task_detail_type2_table_clos[20], constant_area1_input3.getEditValue());
			data.put(DataBaseUtil.task_detail_type2_table_clos[21], constant_area1_input4.getEditValue());
			data.put(DataBaseUtil.task_detail_type2_table_clos[22], constant_area2_input1.getEditValue());
			data.put(DataBaseUtil.task_detail_type2_table_clos[23], constant_area2_input2.getEditValue());
			data.put(DataBaseUtil.task_detail_type2_table_clos[24], constant_area2_input3.getEditValue());
			data.put(DataBaseUtil.task_detail_type2_table_clos[25], constant_area2_input4.getEditValue());
		}
		return data;
	}
}
